package com.chenfan.common.extension.spring;

import org.springframework.lang.NonNull;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.concurrent.Executor;

/**
 * 事务工具类
 * @author wulg
 * @date 2023/11/17
 */
public class TransactionUtils {

    /**
     * 事务完成后同步执行
     * @param runnable
     */
    public static void afterCompletion(@NonNull Runnable runnable) {
        if(TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCompletion(int status) {
                    runnable.run();
                }
            });
        } else {
            runnable.run();
        }
    }

    /**
     * 事务完成后异步执行
     * @param runnable
     */
    public static void afterCompletion(@NonNull Executor executor, @NonNull Runnable runnable) {
        if(TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCompletion(int status) {
                    executor.execute(runnable);
                }
            });
        } else {
            executor.execute(runnable);
        }
    }

    /**
     * 事务提交后同步执行
     * @param runnable
     */
    public static void afterCommit(@NonNull Runnable runnable) {
        if(TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    runnable.run();
                }
            });
        } else {
            runnable.run();
        }
    }

    /**
     * 事务提交后异步执行
     * @param runnable
     */
    public static void afterCommit(@NonNull Executor executor, @NonNull Runnable runnable) {
        if(TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    executor.execute(runnable);
                }
            });
        } else {
            executor.execute(runnable);
        }
    }

    /**
     * 事务回滚后同步运行
     * @param runnable
     */
    public static void afterRollback(@NonNull Runnable runnable) {
        if(!TransactionSynchronizationManager.isActualTransactionActive()) {
            return;
        }
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            @Override
            public void afterCompletion(int status) {
                if (TransactionSynchronization.STATUS_ROLLED_BACK == status) {
                    runnable.run();
                }
            }
        });
    }

    /**
     * 事务回滚后异步运行
     * @param runnable
     */
    public static void afterRollback(@NonNull Executor executor, @NonNull Runnable runnable) {
        if(!TransactionSynchronizationManager.isActualTransactionActive()) {
            return;
        }
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            @Override
            public void afterCompletion(int status) {
                if (TransactionSynchronization.STATUS_ROLLED_BACK == status) {
                    executor.execute(runnable);
                }
            }
        });
    }
}
